using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	using TemplateWeb.Models.Repositories;
	using TemplateWeb.Helpers;
	using System.Web.Security;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectNguoiDung(string role)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_NGUOIDUNG,
				Role = role
			};
			return View(CreateViewModel(data));
		}

		public ActionResult SelectNguoiDungPaging(string role, int page = 1)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_NGUOIDUNG,
				CurrentPage = page,
				Role = role
			};
            return PartialView("Templates/TH_ListNguoiDung", CreateViewModel(data));
		}

		[AcceptVerbs(HttpVerbs.Delete)]
		public JsonResult DeleteNguoiDung(Guid id, string role)
		{
			return Json(new{
				Success = _repNguoiDung.Delete(id),
				RecordCount = _repNguoiDung.GetTotalPage(role),
				DeleteId = id
			});
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateNguoiDung(FormCollection forms)
		{
			string username = forms.GetValues("NguoiDung_UserName")[0];
			string email = forms.GetValues("NguoiDung_Email")[0];
			string id = forms.GetValues("NguoiDung_ID")[0];
			string hoten = forms.GetValues("NguoiDung_HoTen")[0];
			string phone = forms.GetValues("NguoiDung_Phone")[0];
			string diachi = forms.GetValues("NguoiDung_DiaChi")[0];
			string currentpage = forms.GetValues("NguoiDung_CurrentPage")[0];
			var user = Membership.GetUser(username);
			if (email != user.Email)
			{
				user.Email = email;
				Membership.UpdateUser(user);
			}
			var nguoidung = _repNguoiDung.SelectByID(Guid.Parse(id));
			if (nguoidung == null)
			{
				nguoidung = new NguoiDung
				{
					ID = Guid.Parse(id),
					HoTen = hoten,
					Phone = phone,
                    DiaChi = diachi
				};
				DataContext.Instance.NguoiDung.AddObject(nguoidung);
				DataContext.Instance.SaveChanges();
			}
			else
			{
				nguoidung.HoTen = hoten;
				nguoidung.Phone = phone;
				nguoidung.DiaChi = diachi;
			 _repNguoiDung.Save();
			}
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_NGUOIDUNG,
				CurrentPage = int.Parse(currentpage)
			};
			return PartialView("Templates/TH_ListNguoiDung", CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertNguoiDung(FormCollection forms)
		{
			string errorText = "";
            var nguoiDungInfo = new AddNguoiDungViewModel
            {
                Info = new NguoiDungInfo
                {
                    BaseInfo = new NguoiDungBaseInfo
                    {
                        UserName = forms.GetValues("NguoiDung_UserName")[0],
                        Password = forms.GetValues("NguoiDung_Password")[0],
                        Email = forms.GetValues("NguoiDung_Email")[0],
                        Role = forms.GetValues("NguoiDung_Role")[0]
                    },
                    ExtraInfo = new NguoiDung
                    {
                        HoTen = forms.GetValues("NguoiDung_HoTen")[0],
                        DiaChi = forms.GetValues("NguoiDung_DiaChi")[0],
                        Phone = forms.GetValues("NguoiDung_Phone")[0]
                    }
                }
            };
			var result = _repNguoiDung.Insert(nguoiDungInfo, ref errorText);
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_NGUOIDUNG,
				Role = forms.GetValues("NguoiDung_OwnerRole")[0]
			};
			return Json(new{
				Success = result,
				Message = "A new item has been added!",
				PartialViewHtml = RenderPartialViewToString("Templates/TH_ListNguoiDung", CreateViewModel(data))
			});
		}

		public ActionResult DetailOfNguoiDung(Guid id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_NGUOIDUNG,
				GuidID =id
			};
			return View(CreateViewModel(data));
		}

	}
}